﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Zyg.Utility.Helper;
using System.Collections.Generic;

namespace Zyg.SSO.Web
{
    public partial class MaintanceAccountAppUsers : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bool IsValid = true;
                Guid APID = Request.QueryString["APID"].ToGuid(out IsValid);
                // Guid APID = new Guid("e30d5c99-be0e-418d-9a31-800c262361cb");
                Guid AccountID = base.Account.Id;

                if (IsValid)
                {
                    APIDHF.Value = APID.ToString();
                    AccountIDHF.Value = AccountID.ToString();

                    GridView_DataBind(AccountID, APID);
                }
                else
                {
                    throw new Exception("您访问的页面无效");
                }
            }
        }

        protected void GridView_DataBind(Guid accountid, Guid appid)
        {
            Zyg.SSO.Core.Business.AccoutAppUserCollection accAppUserColl = Zyg.SSO.Core.Business.AccountApp.Load(accountid, appid).Users;
            List<Zyg.SSO.Core.Business.AccountAppUser> accAppUserList = new List<Zyg.SSO.Core.Business.AccountAppUser>();
            for (int i = 0; i < accAppUserColl.Count; i++)
            {
                accAppUserList.Add(accAppUserColl[i]);
            }

            AccountAppUserGridView.DataSource = accAppUserList;
            AccountAppUserGridView.DataBind();
        }

        protected void AccountAppUserGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            AccountAppUserGridView.PageIndex = e.NewPageIndex;
            GridView_DataBind(new Guid(AccountIDHF.Value), new Guid(APIDHF.Value));
        }

        protected void BtnAdd_OnClick(object sender, EventArgs e)
        {
            Zyg.SSO.Core.Business.AccountAppUser accAppUser = new Zyg.SSO.Core.Business.AccountAppUser();
            Zyg.SSO.Core.Business.AccountApp accountApp = Zyg.SSO.Core.Business.AccountApp.Load(new Guid(AccountIDHF.Value), new Guid(APIDHF.Value));
            accAppUser.Id = Guid.NewGuid();
            accAppUser.Name = NewName.Text.Trim();
            accAppUser.Password = NewPass.Text.Trim();
            accAppUser.DateCreated = DateTime.Now;
            accAppUser.AccountAppID = accountApp.Id;

            try
            {
                accAppUser.Save();
                WriteBackScript("alert('添加成功!')");
                GridView_DataBind(new Guid(AccountIDHF.Value), new Guid(APIDHF.Value));
                NewName.Text = "";
                NewPass.Text = "";
            }
            catch
            {
                //throw new Exception("添加失败!");
            }

        }

        protected void BtnModify_OnClick(object sender, EventArgs e)
        {
            Zyg.SSO.Core.Business.AccountAppUser accAppUser = Zyg.SSO.Core.Business.AccountAppUser.Load(new Guid(AccountAppUserIDHF.Value));

            accAppUser.Password = ModifyPass.Text.Trim();
            ModifyPass.Text = "";

            try
            {
                accAppUser.Save();
                WriteBackScript("alert('修改成功!')");
                GridView_DataBind(new Guid(AccountIDHF.Value), new Guid(APIDHF.Value));
            }
            catch
            {
                throw new Exception("修改失败!");
            }
        }

        protected void BtnDelete_Click(object sender, EventArgs e)
        {
            bool hasApp = false;

            for (int i = AccountAppUserGridView.Rows.Count - 1; i > -1; i--)
            {
                CheckBox cb = AccountAppUserGridView.Rows[i].FindControl("SelectCB") as CheckBox;

                if (cb.Checked)
                {
                    Guid accountAppUserId = new System.Guid(AccountAppUserGridView.DataKeys[i].Value.ToString());

                    Zyg.SSO.Core.Business.AccountAppUser accountAppUser = Zyg.SSO.Core.Business.AccountAppUser.Load(accountAppUserId);
                    accountAppUser.DeleteOnSave();
                    accountAppUser.Save();
                }
            }

            GridView_DataBind(new Guid(AccountIDHF.Value), new Guid(APIDHF.Value));

            if (hasApp)
                WriteBackScript("alert('删除成功')");
        }

    }
}
